﻿using Kingdee.BOS.Core.DynamicForm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using Kingdee.BOS.Core.Bill.PlugIn.Args;
using Kingdee.BOS.Core.Metadata.FormElement;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.JSON;
using Kingdee.BOS.App.Data;
using System.Data;
using System.Web;
namespace LiMinCRMPlugin
{
    /// <summary>
    /// 生产订单表单插件      
    /// wzx
    /// 2024年5月11日
    /// </summary>
    [System.ComponentModel.Description("生产订单表单插件")]
    [HotUpdate]
    public class POForm : AbstractBillPlugIn
    {
        public override void EntryButtonCellClick(EntryButtonCellClickEventArgs e)
        {
            base.EntryButtonCellClick(e);
            if (e.FieldKey.EqualsIgnoreCase("F_Dev_Technical"))
            {
                var UserID = this.Context.UserId;
                var UserAccount = "";
                string sql_User = string.Format(@"select FUserAccount  from T_SEC_USER where FUSERID='{0}' and FFORBIDSTATUS='A'", UserID);
                DataTable dt_User = DBUtils.ExecuteDataSet(this.Context, sql_User).Tables[0];
                if (dt_User.Rows.Count > 0)
                {
                    UserAccount = Convert.ToString(dt_User.Rows[0]["FUserAccount"]);
                }

                // 生成Url地址
                var url = this.Model.GetValue("F_Dev_Technical", e.Row) as string;
                if (url != "" && url != null && url != " ")
                {


                    string userid = "";
                    string useridUtf8 = ""; // url编码
                    if (url.IndexOf("http%") >= 0)
                    {
                        userid = "&loginid=CRM";
                        useridUtf8 = HttpUtility.UrlEncode(userid, Encoding.UTF8); // url编码
                        url = url + userid; //UserAccount;
                    }
                    else
                    {
                        userid = "&loginid=CRM";

                        string bb = "http://oa.limincloud.com:8888/api/limin-custom/sso/redirect?redirectUrl=";

                        string aaa = url.Replace(bb, "");


                        useridUtf8 = HttpUtility.UrlEncode(aaa, Encoding.UTF8); // url编码
                        url = bb + useridUtf8 + userid; //UserAccount;
                    }



                    //url = url + "&loginid=" + UserAccount; //UserAccount;
                    //url = HttpUtility.UrlEncode(url, Encoding.UTF8); // url编码
                    // 方式1：直接打开浏览器
                    //this.View.AddAction("ShowWebURL", url);
                    // 方式2：打开Url地址
                    var webobj = new JSONObject();
                    webobj["source"] = url;
                    webobj["height"] = 768;
                    webobj["width"] = 1024;
                    webobj["isweb"] = false; // 是否新弹出一个浏览器窗口（or选项卡）打开网页地址
                    webobj["title"] = "技术指标确认";
                    this.View.AddAction("ShowKDWebbrowseForm", webobj);
                    this.View.SendDynamicFormAction(this.View);
                }

            }
        }
    }
}
